package servlet.admin;

import entity.Pet;
import service.Impl.PetServiceImpl;
import service.PetService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/admin/PetEdit")
public class PetEditServlet extends HttpServlet {
    private PetService petService = new PetServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        Integer id = Integer.parseInt(req.getParameter("id"));
        String petName = req.getParameter("petName");
        String petType = req.getParameter("petType");
        String sex = req.getParameter("sex");
        String birthdayStr = req.getParameter("birthday");
        String pic = req.getParameter("pic");
        String stateStr = req.getParameter("state");
        String remark = req.getParameter("remark");
        Pet pet = new Pet();
        pet.setId(id);
        pet.setPetName(petName);
        pet.setPetType(petType);
        pet.setSex(sex);
        pet.setBirthday(java.sql.Date.valueOf(birthdayStr));
        pet.setPic(pic);
        pet.setState(Integer.parseInt(stateStr));
        pet.setRemark(remark);
        petService.updatePet(pet);
        resp.sendRedirect(req.getContextPath() + "/admin/PetList");
    }
}
